﻿using System;
using System.Collections.Generic;
using System.Text;

namespace VkSample98
{
    public class SkydomeData
    {
        public uint VCount { get; set; }

        public float[] GenData()
        {
            float radius = (float)(0.82 * 51 * 30.0);
            float ANGLE_SPAN = 18;
            float angleV = 90;
            List<float> texCoorArray = GenerateSkyTexCoor((int)(360 / ANGLE_SPAN), (int)(angleV / ANGLE_SPAN));
            int tc = 0;
            int ts = texCoorArray.Count;
			List<float> alVertix = new List<float>();
			List<float> alTexCoor = new List<float>();
            for(float vAngle = angleV; vAngle > 0; vAngle -= ANGLE_SPAN)
            {
                for(float hAngle = 360; hAngle > 0; hAngle -= ANGLE_SPAN)
                {
					double xozLength = radius * Math.Cos(toRadians(vAngle)); //获取xz 平面参数
					float x1 = (float)(xozLength * Math.Cos(toRadians(hAngle))); //获取X 坐标
					float z1 = (float)(xozLength * Math.Sin(toRadians(hAngle))); //获取Z 坐标
					float y1 = (float)(radius * Math.Sin(toRadians(vAngle))); //获取Y 坐标
					xozLength = radius * Math.Cos(toRadians(vAngle - ANGLE_SPAN));
					float x2 = (float)(xozLength * Math.Cos(toRadians(hAngle)));
					float z2 = (float)(xozLength * Math.Sin(toRadians(hAngle)));
					float y2 = (float)(radius * Math.Sin(toRadians(vAngle - ANGLE_SPAN)));
					xozLength = radius * Math.Cos(toRadians(vAngle - ANGLE_SPAN));
					float x3 = (float)(xozLength * Math.Cos(toRadians(hAngle - ANGLE_SPAN)));
					float z3 = (float)(xozLength * Math.Sin(toRadians(hAngle - ANGLE_SPAN)));
					float y3 = (float)(radius * Math.Sin(toRadians(vAngle - ANGLE_SPAN)));
					xozLength = radius * Math.Cos(toRadians(vAngle));
					float x4 = (float)(xozLength * Math.Cos(toRadians(hAngle - ANGLE_SPAN)));
					float z4 = (float)(xozLength * Math.Sin(toRadians(hAngle - ANGLE_SPAN)));
					float y4 = (float)(radius * Math.Sin(toRadians(vAngle)));
					alVertix.Add(x1); alVertix.Add(y1); alVertix.Add(z1);
					alVertix.Add(x4); alVertix.Add(y4); alVertix.Add(z4);
					alVertix.Add(x2); alVertix.Add(y2); alVertix.Add(z2);
					alVertix.Add(x2); alVertix.Add(y2); alVertix.Add(z2);
					alVertix.Add(x4); alVertix.Add(y4); alVertix.Add(z4);
					alVertix.Add(x3); alVertix.Add(y3); alVertix.Add(z3);
					alTexCoor.Add(texCoorArray[tc++ % ts]);
					alTexCoor.Add(texCoorArray[tc++ % ts]);
					alTexCoor.Add(texCoorArray[tc++ % ts]);
					alTexCoor.Add(texCoorArray[tc++ % ts]);
					alTexCoor.Add(texCoorArray[tc++ % ts]);
					alTexCoor.Add(texCoorArray[tc++ % ts]);
					alTexCoor.Add(texCoorArray[tc++ % ts]);
					alTexCoor.Add(texCoorArray[tc++ % ts]);
					alTexCoor.Add(texCoorArray[tc++ % ts]);
					alTexCoor.Add(texCoorArray[tc++ % ts]);
					alTexCoor.Add(texCoorArray[tc++ % ts]);
					alTexCoor.Add(texCoorArray[tc++ % ts]);
				}
            }
			VCount = (uint)(alVertix.Count / 3);
			float[] vdata = new float[VCount * 5];
			int index = 0;
			for(int i = 0; i < VCount; i++)
			{
				vdata[index++] = alVertix[i * 3 + 0]; //保存顶点X 坐标
				vdata[index++] = alVertix[i * 3 + 1]; //保存顶点Y 坐标
				vdata[index++] = alVertix[i * 3 + 2]; //保存顶点Z 坐标
				vdata[index++] = alTexCoor[i * 2 + 0]; //保存纹理S 坐标
				vdata[index++] = alTexCoor[i * 2 + 1]; //保存纹理T 坐标
			}
			return vdata;
        }

        public List<float> GenerateSkyTexCoor(int bw, int bh)
        {
			List<float> result = new List<float>();
			float sizew = 1.0f / bw;
			float sizeh = 1.0f / bh;
			for (int i = 0; i < bh; i++)
			{
				for (int j = 0; j < bw; j++)
				{
					float s = j * sizew;
					float t = i * sizeh;
					result.Add(s);
					result.Add(t);
					result.Add(s + sizew);
					result.Add(t);
					result.Add(s);
					result.Add(t + sizeh);
					result.Add(s);
					result.Add(t + sizeh);
					result.Add(s + sizew);
					result.Add(t);
					result.Add(s + sizew);
					result.Add(t + sizeh);
				}
			}
			return result;
		}

		public double toRadians(double angle) => angle * Math.PI / 180.0;

	}
}
